package com.qf.agro.web.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.qf.agro.common.sys.ResultInfo;
import com.qf.agro.pojo.sys.SysUser;
import com.qf.agro.service.sys.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author: 六六六
 * @create: 2023-06-29 15:25
 **/
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService userService;


    @PostMapping("/login")
    public ResultInfo login(@RequestBody Map<String,String> map){
        String username =  map.get("username");
        String password = map.get("password");
        return  userService.login(username,password);
    }


    @GetMapping("/page")
    public ResultInfo page(@RequestParam(value = "page",defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue ="5") int pageSize, String name){
        Page<SysUser> page = userService.page(pageNum, pageSize, name);
        return ResultInfo.success(page);
    }

    @PostMapping("/checkUsername")
    public  ResultInfo checkUsername(@RequestBody Map<String,String> map){
        String username = map.get("username");
        boolean b = userService.checkUsername(username);
        if (b) {
            return ResultInfo.success("用吧,这个用户名哥让你用");
        } else {
            return  ResultInfo.error("不好意思,这个用户名哥不能给你用");
        }
    }

    @PostMapping
    public ResultInfo save(@RequestBody SysUser sysUser){
        userService.save(sysUser);
        return ResultInfo.success("添加成功");
    }

    @PutMapping
    public  ResultInfo update(@RequestBody SysUser sysUser){
        userService.update(sysUser);
        return ResultInfo.success("修改成功");
    }

    /**
     * 禁用用户
     * @param id
     * @return
     */
    @DeleteMapping
    public ResultInfo delete(Long id){
        //1.调用业务层停用
        userService.disabled(id);
        //2.返回
        return ResultInfo.success(null);
    }

}
